package com.efast.dmom.mgmt.service.les.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.dao.les.TwBasPartBatchDao;
import com.efast.dmom.mgmt.dao.les.TwBasPartBatchHeaderDao;
import com.efast.dmom.mgmt.entity.les.TwBasPartBatch;
import com.efast.dmom.mgmt.entity.les.TwBasPartBatchHeader;
import com.efast.dmom.mgmt.entity.master.TmBasBatch;
import com.efast.dmom.mgmt.service.les.TwBasPartBatchHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

@Service
public class TwBasPartBatchHeaderServiceImpl implements TwBasPartBatchHeaderService{

	@Autowired
	private TwBasPartBatchHeaderDao twBasPartBatchHeaderDao;

	@Autowired
	private TwBasPartBatchDao twBasPartBatchDao;

	@Override
	public void insertNonEmptyTwBasPartBatchHeader(TwBasPartBatchHeader value) {
		List<TmBasBatch> listTmBasBatch = twBasPartBatchHeaderDao.queryPartBatchByBatchGroupId(value.getBatchGroupId());
		List<TwBasPartBatchHeader> list = twBasPartBatchHeaderDao.selectTwBasPartBatchHeader(new Assist(Assist.andEq("part_no_version", value.getPartNoVersion())));
		if (list !=null && list.size()>0) {
			throw new ServiceException(value.getPartNoVersion()+"已经存在批次属性组");
		}
		value.setCompanyCode(SpringWebUtils.getRequestCompany());
		twBasPartBatchHeaderDao.insertNonEmptyTwBasPartBatchHeader(value);
		if (listTmBasBatch!=null) {
			for (TmBasBatch tmBasBatch : listTmBasBatch) {
				TwBasPartBatch twBasPartBatch=new TwBasPartBatch();
				BeanUtils.copyProperties(tmBasBatch, twBasPartBatch);
				twBasPartBatch.setPartNoVersion(value.getPartNoVersion());
				twBasPartBatch.setSupplNo(value.getSupplNo());
				twBasPartBatch.setTwBasPartBatchHeaderId(value.getTwBasPartBatchHeaderId());
				twBasPartBatch.setTmBasBatchGroupId(value.getBatchGroupId());
				twBasPartBatch.setTrBasPartSupplId(value.getTrBasPartSupplId());
				twBasPartBatch.setCompanyCode(SpringWebUtils.getRequestCompany());
				twBasPartBatch.setCreateUser(SpringWebUtils.getRequestUser());
				twBasPartBatch.setCreateTime(new Date());
				twBasPartBatch.setMarkStatus(1);
				twBasPartBatchDao.insertNonEmptyTwBasPartBatch(twBasPartBatch);
			}
		}
	}

	
	@Override
	public long getTwBasPartBatchHeaderRowCount(Assist assist) {
		return twBasPartBatchHeaderDao.getTwBasPartBatchHeaderRowCount(assist);
	}

	@Override
	public List<TwBasPartBatchHeader> selectTwBasPartBatchHeader(Assist assist) {
		return twBasPartBatchHeaderDao.selectTwBasPartBatchHeader(assist);
	}

	@Override
	public TwBasPartBatchHeader selectTwBasPartBatchHeaderByObj(TwBasPartBatchHeader obj) {
		return twBasPartBatchHeaderDao.selectTwBasPartBatchHeaderByObj(obj);
	}

	@Override
	public TwBasPartBatchHeader selectTwBasPartBatchHeaderById(Integer id) {
		return twBasPartBatchHeaderDao.selectTwBasPartBatchHeaderById(id);
	}

	@Override
	public int insertTwBasPartBatchHeader(TwBasPartBatchHeader value) {
		return twBasPartBatchHeaderDao.insertTwBasPartBatchHeader(value);
	}

	@Override
	public int insertTwBasPartBatchHeaderByBatch(List<TwBasPartBatchHeader> value) {
		return twBasPartBatchHeaderDao.insertTwBasPartBatchHeaderByBatch(value);
	}

	@Override
	public int deleteTwBasPartBatchHeaderById(Integer id) {
		return twBasPartBatchHeaderDao.deleteTwBasPartBatchHeaderById(id);
	}

	@Override
	public int deleteTwBasPartBatchHeader(Assist assist) {
		return twBasPartBatchHeaderDao.deleteTwBasPartBatchHeader(assist);
	}

	@Override
	public int updateTwBasPartBatchHeaderById(TwBasPartBatchHeader enti) {
		return twBasPartBatchHeaderDao.updateTwBasPartBatchHeaderById(enti);
	}

	@Override
	public int updateTwBasPartBatchHeader(TwBasPartBatchHeader value, Assist assist) {
		return twBasPartBatchHeaderDao.updateTwBasPartBatchHeader(value, assist);
	}

	@Override
	public int updateNonEmptyTwBasPartBatchHeaderById(TwBasPartBatchHeader enti) {
		return twBasPartBatchHeaderDao.updateNonEmptyTwBasPartBatchHeaderById(enti);
	}

	@Override
	public int updateNonEmptyTwBasPartBatchHeader(TwBasPartBatchHeader value, Assist assist) {
		return twBasPartBatchHeaderDao.updateNonEmptyTwBasPartBatchHeader(value, assist);
	}

	public TwBasPartBatchHeaderDao getTwBasPartBatchHeaderDao() {
		return this.twBasPartBatchHeaderDao;
	}

	public void setTwBasPartBatchHeaderDao(TwBasPartBatchHeaderDao twBasPartBatchHeaderDao) {
		this.twBasPartBatchHeaderDao = twBasPartBatchHeaderDao;
	}

}